/*
 * @brief:
 *
 * @author: dariaye@tencent.com
 * @date: 2012-6-12
 * @version: 1.0
 * @changes:
 *    <author>        <time>        <version >        <desc>
 *     dariaye             2012-6-12              1.0                 Create
 */

#ifndef WALLE_COMMON_UTIL_H
#define WALLE_COMMON_UTIL_H

#include <stdlib.h>
#include <mpi.h>

#include "global_def.h"

#define CHKERRQ(info) if(info != MPI_SUCCESS) { \
    WALLE::Printf(MPI_COMM_WORLD, "Error: %s in %s:%d. Return value is %d", \
                              __FUNCTION__, __FILE__, __LINE__, info); \
    MPI_Abort(MPI_COMM_WORLD, -1); }

WALLE_BEGIN

void FPrintf(MPI_Comm comm, FILE* fp, const char format[],...);
void SyncPrintf(MPI_Comm comm,const char format[],...);
void Printf(MPI_Comm comm,const char format[],...);
void SetAffinity(int cpu_id);

template <typename T>
T SyncReturn(MPI_Comm comm, T result)
{
    // Sync Return should wait for every processor to be ok
    MPI_Barrier(comm);
    // then return result
    return result;
}

WALLE_END

#endif /* WALLE_COMMON_UTIL_H */
